package com.pe1n.yangtaopai.view.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.adapter.ParentAdapter;
import com.pe1n.yangtaopai.base.BaseFragment;
import com.pe1n.yangtaopai.bean.LeftMenuBean;
import com.pe1n.yangtaopai.bean.SubsData;
import com.pe1n.yangtaopai.helper.InitRetrofitHelper;
import com.pe1n.yangtaopai.retrofitApi.RetrofitApi;
import com.pe1n.yangtaopai.utils.URLConstant;

import java.util.ArrayList;


import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

 public class  LeftMenuFragment extends BaseFragment implements ExpandableListView.OnGroupExpandListener, ParentAdapter.OnChildTreeViewClickListener {
    private RetrofitApi mRetrofit;
    private ArrayList<LeftMenuBean.DataData> parents;//组数据
    private ExpandableListView eList;
    private Context mContext;
    private final String TAG = "best";
    private ParentAdapter adapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.activity_left_menu, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        eList = (ExpandableListView) view.findViewById(R.id.eList);
        parents = new ArrayList<>();
        mRetrofit = InitRetrofitHelper.getRetrofitInterfance();
        mRetrofit.getLeftMenuBean(URLConstant.LEFT_MENU).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LeftMenuBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(LeftMenuBean leftMenuBean) {
                        Log.e("TAG", String.valueOf(leftMenuBean.getData().size()));
                        parents.addAll(leftMenuBean.getData());
                        initEList();
                    }
                });
    }




    private void initEList() {

        /// eList = (ExpandableListView) findViewById(R.id.eList);
        eList.setGroupIndicator(null);
        eList.setOnGroupExpandListener(this);

        adapter = new ParentAdapter(mContext, parents);
        eList.setAdapter(adapter);
        adapter.setOnChildTreeViewClickListener(this);

    }

    @Override
    public void onGroupExpand(int groupPosition) {
        for (int i = 0; i < parents.size(); i++) {
            if (i != groupPosition) {
                eList.collapseGroup(i);
            }
        }
    }

    @Override
    public void onClickPosition(int parentPosition, int groupPosition, int childPosition) {
           String name=parents.get(parentPosition).getSubs().get(groupPosition).getSubs().get(childPosition).getCName();
           String id=parents.get(parentPosition).getSubs().get(groupPosition).getSubs().get(childPosition).getId();
           Intent intent=new Intent( mContext,ItemTwoListActivity.class);
            intent.putExtra("name",name);
            intent.putExtra("id",id);
            mContext.startActivity( intent);

    }
}



